#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# generate keys

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/asym_enc_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str


str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture $permutation_str \
    --make-start-point -start-point $output_dir/asym_enc_start_point_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -start-point $output_dir/asym_enc_start_point_file.xf -monomial-key $output_dir/asym_enc_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/asym_enc_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# encrypt

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -binomial-key $output_dir/asym_enc_binomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --encrypt-asymmetric -plain-text $lorem_ipsum_file -cipher-text $output_dir/asym_enc_lorem_ipsum_file
"

echo ""; echo $str; echo ""
eval $str

# decrypt

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/asym_enc_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --decipher-asymmetric -plain-text $output_dir/asym_enc_lorem_ipsum_file_decyphered -cipher-text $output_dir/asym_enc_lorem_ipsum_file
"

echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/asym_enc_lorem_ipsum_file_decyphered $lorem_ipsum_file && echo 'PASS' || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

# permute cypher

cat $output_dir/asym_enc_lorem_ipsum_file | sed 's/$/?/' > $output_dir/asym_enc_lorem_ipsum_file_wrong 

# perform decryption

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/asym_enc_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-error-log $output_dir/wrong_lib_info_log_file.xf \
    --decipher-asymmetric -plain-text $output_dir/asym_enc_lorem_ipsum_file_decyphered_wrong -cipher-text $output_dir/asym_enc_lorem_ipsum_file_wrong "

echo ""; echo $str; echo ""
eval $str

str=" 
if [ ! -s $output_dir/wrong_lib_info_log_file.xf ] ; then echo 'FAIL' ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/asym_enc_lorem_ipsum_file_decyphered_wrong $lorem_ipsum_file && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str
